<?php
namespace app\finance\model;
use think\Db;
use app\common\model\Common;
class Income extends Common
{
	/**
	* 收入记账-列表
	*/
	public function incomeList($orderWhere, $repairWhere, $page, $row, $ty)
	{
		$data = [];
		$orderSql = $this->orderCount($orderWhere);
		$repairSql = $this->repairCount($repairWhere);
		if($ty == ''){
			$data = $this->orderRepair($orderSql, $repairSql, $page, $row);
		}else if($ty == 'order'){
			$data = Db::table($orderSql.' as o')->page($page, $row)->select();
		}else if($ty == 'repair'){
			$data = Db::table($repairSql)->page($page, $row)->select();
		}
		if(empty($data)) return [];
		foreach($data as $k => $v){
			$data[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
			$data[$k]['shop_name'] = Db::name('shop')->where('shop_id',$v['shop_id'])->value('shop_name');
			$data[$k]['state_text'] = $v['state'] == 0 ? '待支付': '已支付';
		}
		return $data;
	}
	/**
	 * 收入记账-统计
	 */
	public function incomeSum($orderWhere, $repairWhere, $ty)
	{
		$ototalMoeny = $this->ototalMoeny($orderWhere);
		$ocostMoney = $this->ocostMoney($orderWhere);
		$oprofitMoeny = $this->oprofitMoeny($orderWhere);
		$ostayMoeny = $this->ostayMoeny($orderWhere);

		$rtotalMoeny = $this->rtotalMoeny($repairWhere);
		$rcostMoney = $this->rcostMoney($repairWhere);
		$rprofitMoeny = $this->rprofitMoeny($repairWhere);
		$rstayMoeny = $this->rstayMoeny($repairWhere);
		if($ty == ''){
			$data['total_moeny'] = ( intval(strval($ototalMoeny*100)) + intval(strval($rtotalMoeny*100)) )/100;
			$data['cost_money'] = ( intval(strval($ocostMoney*100)) + intval(strval($rcostMoney*100)) )/100;
			$data['profit_moeny'] = ( intval(strval($oprofitMoeny*100)) + intval(strval($rprofitMoeny*100)) )/100;
			$data['stay_moeny'] = ( intval(strval($ostayMoeny*100)) + intval(strval($rstayMoeny*100)) )/100;
			return $data;
		}else if($ty == 'order'){
			$data['total_moeny'] = $ototalMoeny;
			$data['cost_money'] = $ocostMoney;
			$data['profit_moeny'] = $oprofitMoeny;
			$data['stay_moeny'] = $ostayMoeny;
			return $data;
		}else if($ty == 'repair'){
			$data['total_moeny'] = $rtotalMoeny;
			$data['cost_money'] = $rcostMoney;
			$data['profit_moeny'] = $rprofitMoeny;
			$data['stay_moeny'] = $rstayMoeny;
			return $data;
		}
		$data['total_moeny'] = 0;
		$data['cost_money'] = 0;
		$data['profit_moeny'] = 0;
		$data['stay_moeny'] = 0;
		return $data;
	}
	/**
	* 收入记账-总条数
	*/
	public function incomeCount($orderWhere, $repairWhere, $ty)
	{
		$orderNum = $this->orderNum($orderWhere);
		$repairNum = $this->repairNum($repairWhere);
		if($ty == ''){
			return intval(strval($orderNum)) + intval(strval($repairNum));
		}else if($ty == 'order'){
			return intval(strval($orderNum));
		}else if($ty == 'repair'){
			return intval(strval($repairNum));
		}
		return 0;
	}
	/**************************************** 私有 *****************************************/
	/**
	* 售货订单总额
	*/
	private function ototalMoeny($where)
	{
		return Db::name('order')->alias('o')->where($where)->sum('o.order_money');
	}
	/**
	* 售货订单总成本
	*/
	private function ocostMoney($where)
	{
		return Db::name('order')->alias('o')
		->join('mps_order_goods og','o.order_id = og.order_id','LEFT')
		->where($where)->sum('og.cost_money');
	}
	/**
	* 售货订单总收入
	*/
	private function oprofitMoeny($where)
	{
		return Db::name('order')->alias('o')
		->join('mps_order_goods og','o.order_id = og.order_id','LEFT')
		->where($where)->sum('og.profit_money');
	}
	/**
	* 售货订单待支付总额
	*/
	private function ostayMoeny($where)
	{
		return Db::name('order')->alias('o')->where($where)->sum('o.stay_money');
	}

	/**
	* 维修订单总额
	*/
	private function rtotalMoeny($where)
	{
		return Db::name('repair')->alias('r')->where($where)->sum('r.repair_money');
	}
	/**
	* 维修订单总成本
	*/
	private function rcostMoney($where)
	{
		return Db::name('repair')->alias('r')->where($where)->sum('r.parts_money');
	}
	/**
	* 维修订单总收入
	*/
	private function rprofitMoeny($where)
	{
		return Db::name('repair')->alias('r')->where($where)->sum('r.people_money');
	}
	/**
	* 维修订单待支付总额
	*/
	private function rstayMoeny($where)
	{
		return Db::name('repair')->alias('r')->where($where)->sum('r.stay_money');
	}

	/**
	* 售货订单统计条数
	*/
	private function orderNum($where)
	{
		return Db::name('order')->alias('o')->where($where)->count();
	}
	/**
	* 维修单统计条数
	*/
	private function repairNum($where)
	{
		return Db::name('repair')->alias('r')->where($where)->where('r.state','in','3,4,5,6,8')->count();
	}
	/**
	 * 收货单、维修单统计
	 */
	private function orderRepair($orderSql, $repairSql, $page, $row)
	{
		$field = 'addtime,order_id,or_sn,shop_id,username,user_sn,phone,order_ty,o_ty,total_money,cost_money,profit_money,state,stay_money';
		$sql = Db::table($repairSql)->field($field)->union($orderSql)->select(false);
		return Db::table("($sql) as a")->field($field)->page($page, $row)->select();
	}
	/**
	* 售货订单统计sql
	*/
	private function orderCount($where)
	{
		$sql = Db::name('order')->alias('o')
		->join('mps_order_goods og','o.order_id = og.order_id','LEFT')
		->field('o.addtime,o.order_id,o.order_sn as or_sn,o.shop_id,o.username,o.user_sn,o.phone,"销售订单" as order_ty,1 as o_ty,o.order_money as total_money,sum(og.cost_money) as cost_money,sum(og.profit_money) as profit_money,o.state,o.stay_money')
		->where($where)->group('o.order_id')->select(false);
		return "($sql)";
	}
	/**
	* 维修单统计sql
	*/
	private function repairCount($where)
	{
		$sql = Db::name('repair')->alias('r')
		->field('r.addtime,r.repair_id as order_id,r.repair_sn as or_sn,r.shop_id,r.username,r.user_sn,r.phone,"维修订单" as order_ty,2 as o_ty,r.repair_money as total_money,r.parts_money as cost_money,r.people_money as profit_money,r.pay_state as state,r.stay_money')
		->where($where)
		->where('state','in',"3,4,5,6,8")
		->select(false);
		return "($sql) as r";
	}
}